package com.tbit.main.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.*;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserConfig;
import com.tbit.main.service.AccountUserConfigService;
import com.tbit.main.service.RedisService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运维人员语音配置
 *
 * @author LMY
 * @create 2021-08-25 19:05
 */
@RestController
@RequestMapping("/accountUserConfigController")
public class AccountUserConfigController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserConfigService accountUserConfigService;
    @Autowired
    private RedisService redisService;

    /**
     * 运维人员语音配置添加修改
     *
     * @param token
     * @param accountUserConfig
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.ACCOUNTUSERCONFIG, methods = MethodEnum.ACCOUNT_USER_CONFIG_ADD)
    public JsonResult add(String token, AccountUserConfig accountUserConfig) {
        Assert.notNull(accountUserConfig.getAccountId(), "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        AccountUserConfig accountUserConfigDB = accountUserConfigService.getAccountUserId(accountUser.getAccountUserId());

        if (accountUserConfigDB == null) {
            accountUserConfig.setAccountUserId(accountUser.getAccountUserId());
            accountUserConfig.setAccountUserName(accountUser.getName());
            accountUserConfig.setAccountId(accountUserConfig.getAccountId());
            accountUserConfigService.add(accountUserConfig);
        } else {
            accountUserConfig.setAccountUserId(accountUser.getAccountUserId());
            accountUserConfigService.update(accountUserConfig);

            String pushKeyZ = accountUserConfig.getAccountUserId() + "_null_" + RedisConstant.REDIS_WORK_PUSH;
            redisService.add(pushKeyZ, accountUserConfig.getAccountUserId() + "", accountUserConfig.getLabourHZ().equals(0) ? 1 : accountUserConfig.getLabourHZ() * 60);
            String pushKeyN = accountUserConfig.getAccountUserId() + "_" + WorkOrderConstant.workType_HD + "_" + RedisConstant.REDIS_WORK_PUSH;
            redisService.add(pushKeyN, accountUserConfig.getAccountUserId() + "", accountUserConfig.getPowerChangeHZ().equals(0) ? 1 : accountUserConfig.getPowerChangeHZ() * 60);
            String pushKeyH = accountUserConfig.getAccountUserId() + "_" + WorkOrderConstant.workType_NC + "_" + RedisConstant.REDIS_WORK_PUSH;
            redisService.add(pushKeyH, accountUserConfig.getAccountUserId() + "", accountUserConfig.getMoveHZ().equals(0) ? 1 : accountUserConfig.getMoveHZ() * 60);
        }
        return JsonResult.succ();
    }

    /**
     * 根据运维人员id查询
     *
     * @param token
     * @return
     */
    @RequestMapping("/accountUserId")
    public JsonResult getAccountUserId(String token) {
        AccountUser accountUser = tokenService.getAccount(token);
        AccountUserConfig accountUserConfig = accountUserConfigService.getAccountUserId(accountUser.getAccountUserId());
        if (accountUserConfig == null) {
            accountUserConfig = new AccountUserConfig();
            accountUserConfig.setAccountUserId(accountUser.getAccountUserId());
            accountUserConfig.setAccountUserName(accountUser.getName());
            accountUserConfig.setAutoMassageSwitch(false);
            accountUserConfig.setPowerChangeHZ(30);
            accountUserConfig.setMoveHZ(30);
            accountUserConfig.setNotAutoMassageSwitch(false);
            accountUserConfig.setLabourHZ(30);
        }
        return JsonResult.succ(accountUserConfig);
    }

}
